﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Kalman;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace QuDao.Api.Controllers
{
    /// <summary>
    /// 商城模块：订单管理API
    /// </summary>
    [AllowAnonymous]
    public class OrderController : ApiControllerBase
    {
        /// <summary>
        /// 获取订单详情
        /// </summary>
        [HttpGet]
        public OrderDetailsDto GetDetails(long id)
        {
            return OrderBO.Instance.GetDetails(id);
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="tid">租户ID</param>
        /// <param name="uid">当前用户ID，传0可以获取所有用户订单</param>
        /// <param name="status">订单状态：0-全部，10-待付款，20-待审核，30-待发货，100-已完成</param>
        /// <param name="pageIndex">页索引，从1开始</param>
        /// <param name="pageSize">每页记录数，默认10</param>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<OrderDto> GetList([FromHeader] int tid, long uid, int status, int pageIndex = 1, int pageSize = 10)
        {
            return OrderBO.Instance.GetList(tid, uid, status, pageIndex, pageSize);
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="tid">租户ID</param>
        /// <param name="uid">当前用户ID，传0可以获取所有用户订单</param>
        /// <param name="itemList">采购清单</param>
        /// <param name="deductionAmount">余额抵扣金额</param>
        /// <returns>返回新订单ID</returns>
        [HttpPost]
        [CacheByHeader(10, "tid", "uid")]
        [ExecutingLock(ExecutingLockMode.ActionSingle)]
        [AllowAnonymous]
        public long Create([FromHeader] int tid, long uid, List<OrderItemAddDto> itemList, decimal deductionAmount)
        {
            return OrderBO.Instance.Create(tid, uid, itemList, deductionAmount); 
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="orderID">订单ID</param>
        /// <returns></returns>
        [HttpPost]
        public bool Cancel(long orderID)
        {
            OrderBO.Instance.Cancel(orderID);
            return true;
        }

        /// <summary>
        /// 删除订单（只能删除取消状态的订单）
        /// </summary>
        /// <param name="orderID">订单ID</param>
        /// <returns></returns>
        [HttpPost]
        public bool Delete(long orderID)
        {
            OrderBO.Instance.Delete(orderID);
            return true;
        }

        /// <summary>
        /// 确认付款
        /// </summary>
        /// <param name="orderID">订单ID</param>
        /// <param name="payeeAccount">转到哪个账号</param>
        /// <param name="transferDate">转账日期</param>
        /// <param name="tradeNo">交易流水号（可空）</param>
        /// <param name="memo">转账备注（可空）</param>
        /// <param name="transferImage">打款凭证图片上传后返回的OSS地址（先调用File接口上传图片至OSS）</param>
        /// <returns></returns>
        [HttpPost]
        public bool ConfirmPayment(long orderID, string payeeAccount, DateTime transferDate, string tradeNo = "", string memo = "", string transferImage = "")
        {
            OrderBO.Instance.ConfirmPayment(orderID, payeeAccount, transferDate, tradeNo, memo, transferImage);
            return true;
        }

        /// <summary>
        /// 付款审核
        /// </summary>
        /// <param name="uid">审核员，当前用户ID</param>
        /// <param name="orderID">订单ID</param>
        /// <param name="payeeAccount">转到哪个账号（审核时可以修改）</param>
        /// <param name="transferDate">转账日期（审核时可以修改）</param>
        /// <param name="tradeNo">交易流水号（审核时可以修改）</param>
        /// <returns></returns>
        [HttpPost]
        public bool PaymentAuditing(long uid, long orderID, string payeeAccount, DateTime? transferDate, string tradeNo)
        {
            OrderBO.Instance.PaymentAuditing(uid, orderID, payeeAccount, transferDate, tradeNo);
            return true;
        }

        /// <summary>
        /// 退回（撤回）订单（会员由于某种原因不能及时打款，可以退回到待付款状态）
        /// </summary>
        /// <param name="orderID">订单ID</param>
        /// <returns></returns>
        [HttpPost]
        public bool Return(long orderID)
        {
            OrderBO.Instance.Return(orderID);
            return true;
        }

        /// <summary>
        /// 确认发货
        /// </summary>
        /// <param name="uid">发货人，当前用户ID</param>
        /// <param name="orderID">订单ID</param>
        /// <param name="expressCompany">快递公司名称</param>
        /// <param name="expressNo">快递单号</param>
        /// <returns></returns>
        [HttpPost]
        public bool ConfirmDelivery(long uid, long orderID, string expressCompany, string expressNo)
        {
            OrderBO.Instance.ConfirmDelivery(uid, orderID, expressCompany, expressNo);
            return true;
        }



    }
}
